package cn.stylefeng.guns.base;

import cn.stylefeng.guns.GunsApplication;
import cn.stylefeng.guns.modular.yingbao.entity.*;
import cn.stylefeng.guns.modular.yingbao.service.*;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.*;

import static java.util.stream.Collectors.toList;


/**
 * 基础测试类
 *
 * @author stylefeng
 * @Date 2017/5/21 16:10
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GunsApplication.class,webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
//@WebAppConfiguration
//@Transactional //打开的话测试之后数据可自动回滚
public class BaseJunit1 {

  @Autowired
  SysRebateSettingService sysRebateSettingService;

  @Autowired
  SysUserGroupService sysUserGroupService;

  @Autowired
  SysGroupSettingService sysGroupSettingService;

  @Autowired
  SysGroupLogService sysGroupLogService;

  @Autowired
  SysBuildingService sysBuildingService;

  @Autowired
  UserMoneyService userMoneyService;


  @Test
    public void test1() throws Exception {

    SysBuilding sysBuilding = sysBuildingService.getOne ( new QueryWrapper<SysBuilding> ().eq ( "ordinal", "1000001794427368" ) );
    Long userids=1244539627788836865l;
    //获取完成建仓的团队数据
    SysUserGroup sysUserGroup = sysUserGroupService.getOne(new QueryWrapper<SysUserGroup>().eq("userid", userids));
    UserMoney userMoney = userMoneyService.getOne(new QueryWrapper<UserMoney>().eq("userid", userids));
    SysGroupSetting sysSetting = sysGroupSettingService.getOne(new QueryWrapper<SysGroupSetting>().eq("grade", sysUserGroup.getGrade()));

    //获取每个级别属性
    List<SysGroupSetting>  settingList = sysGroupSettingService.list(null);
    //所有下级
    List<String> ascUserids= sysUserGroupService.findAscId(userids);
    String teamId = sysUserGroup.getTeamId();

    if (StringUtils.isEmpty(teamId)){
      return;
    }
    List<String> arrayList = JSONObject.parseObject(teamId, ArrayList.class);
    System.out.println (ascUserids);
    for (int i=0;i<arrayList.size();i++){
      int grade;
      //查询出需要返利的下级单个id
      String userid= arrayList.get(i);
      SysUserGroup sysUserGroupServiceOne = sysUserGroupService.getOne(new QueryWrapper<SysUserGroup>().eq("userid",Long.valueOf(userid) ));

      List<String> descId = sysUserGroupService.findDescId(userid);

      if (descId==null||arrayList==null){
        continue;
      }
      //交集
      List<String> collect2 =descId.stream().filter(num -> ascUserids.contains(num))
              .collect(toList());
      System.err.println ("userid:"+userid);
      System.err.println ("交集："+collect2);
      System.err.println ("时间："+sysBuilding.getCreateTime());
      System.err.println ("时间："+sysBuilding.getBuildingTime ());
      //判断等级并且
      if (collect2.size()>0){
        List<SysBuilding> list = sysBuildingService.list(new QueryWrapper<SysBuilding>().in("userid", collect2)
                .gt("create_time",sysBuilding.getCreateTime()));
        if (list.size()>0){
          List<Long> useridList=new ArrayList();
          list.forEach(sys ->{
            useridList.add(sys.getUserid());
          });
          System.err.println ("可用id:"+useridList);
          List<SysUserGroup> list1 = sysUserGroupService.list(new QueryWrapper<SysUserGroup>().in("userid", useridList));
          System.err.println ("人数："+list1.size ());
          Optional<SysUserGroup> max = list1.stream().max(Comparator.comparingInt(SysUserGroup::getGrade));
          SysUserGroup sysUserGroup1 = max.get();

          grade= sysUserGroup1.getGrade();
        }else {
          grade=sysUserGroupServiceOne.getGrade();
        }
      }else {
        grade=sysUserGroupServiceOne.getGrade();
      }
      SysBuilding one = sysBuildingService.getOne ( new QueryWrapper<SysBuilding> ().eq ( "userid", userid )
              .gt ( "create_time", sysBuilding.getCreateTime () )
              .orderByAsc ( "create_time" ).last ( "limit 1" ) );
      if (one==null){
        continue;
      }
      List<SysGroupLog> list = sysGroupLogService.list (new QueryWrapper<SysGroupLog> ()
              .eq ( "ordinal",one.getOrdinal ()).in("userid",ascUserids));



      int gt=0;
      if (list.size ()>0){
        Optional<SysGroupLog> max = list.stream().max(Comparator.comparingInt(SysGroupLog::getGrade));
        SysGroupLog sysGroupLog = max.get();
        gt= sysGroupLog.getGrade();
      }
      if (gt>=grade){
        grade=gt;
      }
      if (gt>=grade){
        grade=gt;
      }
      int earnings=sysSetting.getEarnings();
      int gi=grade;
      List<SysGroupSetting> collect = settingList.stream().filter(sysGroupSetting ->
              sysGroupSetting.getGrade().equals(gi)).collect(toList());
      int earnings1 = 0;
      if (collect!=null&&collect.size()>0){
        earnings1 = collect.get ( 0 ).getEarnings ();
        earnings=earnings-earnings1;
      }


      long shouyi = one.getPrice () * one.getEarnings () * earnings / 10000;
      System.err.println ("收益："+shouyi);
      System.err.println ("最高等级："+grade);

    }

    }
}
